//
//  MMGVideoPrizeClawView.m
//  MyApp
//
//  Created by Don on 2017/9/4.
//  Copyright © 2017年 com.memego. All rights reserved.
//

#import "MMGVideoPrizeClawView.h"
#import "MMGPrizeClawOpenGLView.h"
#import "MMGJoyStickView.h"
#import "UIColor+HexColor.h"
#import "MMGVideoPrizeClawViewModel.h"
#import "UIImage+CacheViaName.h"
#import "MMGLoadingButton.h"

static const float MMG_BottomContainer_Height = 250;
static const float MMG_BottomColorContainer_Height = 180;
@interface MMGVideoPrizeClawView ()<MMGJoyStickViewDelegate>

@property (nonatomic, strong) MMGVideoPrizeClawViewModel *viewModel;
@property (nonatomic, strong) MMGPrizeClawOpenGLView *prizeClawGLView;

@property (nonatomic, strong) MMGJoyStickView *joyStick;
@property (nonatomic, strong) UIButton *prizeClawButton;
@property (nonatomic, strong) MMGLoadingButton *orderGameButton;
@property (nonatomic, strong) MMGLoadingButton *startGameButton;
@property (nonatomic, strong) UILabel *gameTimeCountDownLabel;

@property (nonatomic, strong) UIButton *camChannelSwitchButton;

@property (nonatomic, strong) UIView *bottomContainer;
@property (nonatomic, strong) UIView *bottomColorBackgroundView;
@end
@implementation MMGVideoPrizeClawView

- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor blackColor];
    
    [self.joyStick setJSBallWithImageName:@"prize_claw_joystick_ball.png"];
    [self.joyStick setJSDockWithImageName:@"prize_claw_joystick_dock.png"];
    [self.joyStick setHitAreaWithRadius:50];

    
    [self.bottomContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(MMG_BottomContainer_Height);
    }];
    
    [self.bottomColorBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bottomContainer);
        make.bottom.mas_equalTo(self.bottomContainer.mas_bottom);
        make.height.mas_equalTo(MMG_BottomColorContainer_Height);
    }];
    
    [self.prizeClawGLView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.bottom.mas_equalTo(self.bottomContainer.mas_top).offset(MMG_BottomContainer_Height-MMG_BottomColorContainer_Height);
        make.left.right.mas_equalTo(self);
    }];
    
    [self.gameTimeCountDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.size.mas_equalTo(CGSizeMake(64, 30));
    }];
    
    //
    [self.camChannelSwitchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bottomColorBackgroundView.mas_top).offset(-20);
        make.right.mas_equalTo(self.bottomColorBackgroundView.mas_right).offset(-20);
        make.size.mas_equalTo(CGSizeMake(54, 54));
    }];
    [self.prizeClawButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bottomColorBackgroundView.mas_right).offset(-20);
        make.top.mas_equalTo(self.joyStick.mas_centerY).offset(-15);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    [self.orderGameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.joyStick.mas_centerY).offset(-15);
        make.centerX.mas_equalTo(self.bottomColorBackgroundView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    [self.startGameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.joyStick.mas_centerY).offset(-15);
        make.centerX.mas_equalTo(self.bottomColorBackgroundView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    self.orderGameButton.enabled = NO;
    self.startGameButton.hidden = YES;
//    self.orderGameButton.hidden = YES;
    self.joyStick.hidden = self.prizeClawButton.hidden = self.gameTimeCountDownLabel.hidden = YES;
    
}

- (void)renderGL:(uint8_t *)frame {
    [self.prizeClawGLView renderGLWithFrame:frame];
}

- (void)bindDataWithViewModel:(MMGBaseViewModel *)viewModel {
    self.viewModel = (MMGVideoPrizeClawViewModel *)viewModel;
    
    MMGWeakSelf(weakSelf);
    [RACObserve(self,self.viewModel.vpcvState) subscribeNext:^(NSNumber *  _Nullable state) {
        DLog(@"state  :%@",state);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            switch (state.intValue) {
                case EMMMGVideoPrizeClawViewStateStartGame:
                {
                    //激活开始游戏按钮
                    weakSelf.orderGameButton.hidden = YES;
                    weakSelf.startGameButton.hidden = NO;
                    weakSelf.startGameButton.enabled = YES;
                    weakSelf.joyStick.hidden = self.prizeClawButton.hidden = self.gameTimeCountDownLabel.hidden = YES;
                }
                    break;
                case EMMMGVideoPrizeClawViewStateJoystick:{
                    
                    weakSelf.startGameButton.hidden = self.orderGameButton.hidden = YES;
                    weakSelf.joyStick.hidden = self.prizeClawButton.hidden = self.gameTimeCountDownLabel.hidden = NO;
                    
                    weakSelf.gameTimeCountDownLabel.text = @"40";
                    [weakSelf.viewModel startCountDownWithTimeSec:40 counting:^(int time) {
                        weakSelf.gameTimeCountDownLabel.text = [NSString stringWithFormat:@"%d",time];
                    } overCount:^(int time) {
                        weakSelf.gameTimeCountDownLabel.text = [NSString stringWithFormat:@"%d",time];
                    }];
                }
                    break;
                case EMMMGVideoPrizeClawViewStateOrderGame: {
                    weakSelf.orderGameButton.enabled = YES;
                    weakSelf.orderGameButton.hidden = NO;
                    weakSelf.startGameButton.hidden = YES;
                    weakSelf.joyStick.hidden = self.prizeClawButton.hidden = self.gameTimeCountDownLabel.hidden = YES;
                }
                    break;
                    
                default:
                    break;
            }
        });
        
        
    }];

//    [RACObserve(self, self.viewModel.videoRenderSize) subscribeNext:^(id  _Nullable x) {
//        [weakSelf.prizeClawGLView setRenderSize:[x CGSizeValue]];
//    }];
}

//TODO: - GAME CONTROL
- (void)orderGame:(UIButton *)button {
    button.enabled = NO;
    if (button == self.orderGameButton) {
        
        [self.viewModel sendGameReserveMsg];
    }else {
        
        [self.viewModel sendGameStartMsg:@"40"];
    }
}

//TODO: - CAM SWITCH
- (void)camSwitch:(UIButton *)button {
    
    if (self.viewModel.cameraChannel < (kMMG_CAMERA_MAXMUM - 1)) {
        self.viewModel.cameraChannel += 1;
    }else {
        self.viewModel.cameraChannel = 0;
    }
    
    
    
}

//TODO: -CLAW PRIZE
- (void)clawPrize:(UIButton *)button {
    [self.viewModel sendStartClawMsg];
}

#pragma mark - joystick delegate
- (void)onMMGJoyStickActivated:(MMGJoyStickView *)joyStick {}
- (void)onMMGJoyStickDeactivated:(MMGJoyStickView *)joyStick {}
- (void)onMMGJoyStickUpdate:(MMGJoyStickView *)joyStick radian:(float)radian directionPoint:(CGPoint)point power:(float)power {
    DLog(@"----------------------");
    //    DLog(@"joystick: %@ , angle: %f  , directionP:%@ , power :%f",joyStick,radian,NSStringFromCGPoint(point),power);
    [self.viewModel sendPrizeClawMachineControlMsgWith:radian];
}

#pragma lazy init
- (MMGPrizeClawOpenGLView *)prizeClawGLView {
    if (!_prizeClawGLView) {
        _prizeClawGLView = [[MMGPrizeClawOpenGLView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 576)];
        [self insertSubview:_prizeClawGLView belowSubview:self.bottomContainer];
    }
    
    return _prizeClawGLView;
}

- (MMGJoyStickView *)joyStick {
    if (!_joyStick) {
        _joyStick = [[MMGJoyStickView alloc] initJoyStickWithFrame:CGRectMake(40, 20, 150, 150) moveAreaRadius:50 followTouch:NO canVisible:YES autoHide:NO animation:YES];
        _joyStick.delegate = self;
        _joyStick.backgroundColor = [UIColor clearColor];
        [self.bottomContainer insertSubview:_joyStick aboveSubview:self.bottomColorBackgroundView];
    }
    
    return _joyStick;
}

- (UIButton *)prizeClawButton {
    if (!_prizeClawButton) {
        _prizeClawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage mmg_imageNamed:@"prize_claw_claw_icon.png"];
        [_prizeClawButton setImage:image forState:UIControlStateNormal];
        [_prizeClawButton addTarget:self action:@selector(clawPrize:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainer insertSubview:_prizeClawButton aboveSubview:self.bottomColorBackgroundView];
    }
    
    return _prizeClawButton;
}

- (MMGLoadingButton *)orderGameButton {
    if (!_orderGameButton) {
        _orderGameButton = [MMGLoadingButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage mmg_imageNamed:@"prize_claw_order_game_icon.png"];
        [_orderGameButton setImage:image forState:UIControlStateNormal];
        [_orderGameButton addTarget:self action:@selector(orderGame:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainer insertSubview:_orderGameButton aboveSubview:self.bottomColorBackgroundView];
    }
    
    return _orderGameButton;
}

- (MMGLoadingButton *)startGameButton {
    if (!_startGameButton) {
        _startGameButton = [MMGLoadingButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage mmg_imageNamed:@"prize_claw_start_game_icon.png"];
        [_startGameButton setImage:image forState:UIControlStateNormal];
        [_startGameButton addTarget:self action:@selector(orderGame:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainer insertSubview:_startGameButton aboveSubview:self.bottomColorBackgroundView];
    }
    
    return _startGameButton;
}
- (UIButton *)camChannelSwitchButton {
    if (!_camChannelSwitchButton) {
        _camChannelSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage mmg_imageNamed:@"prize_clam_camera_switch.png"];
        [_camChannelSwitchButton setImage:image forState:UIControlStateNormal];
        [_camChannelSwitchButton addTarget:self action:@selector(camSwitch:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainer insertSubview:_camChannelSwitchButton aboveSubview:self.bottomColorBackgroundView];
        
    }
    return _camChannelSwitchButton;
}

- (UIView *)bottomContainer {
    if (!_bottomContainer) {
        _bottomContainer = [[UIView alloc] init];
        
        _bottomContainer.backgroundColor = [UIColor clearColor];
        [self addSubview:_bottomContainer];
    }
    return _bottomContainer;
}

- (UIView *)bottomColorBackgroundView {
    if (!_bottomColorBackgroundView) {
        _bottomColorBackgroundView = [[UIView alloc] init];
        _bottomColorBackgroundView.backgroundColor = [UIColor mmg_colorWithHex:kMMGColorHex_MainColor];
        
        [self.bottomContainer addSubview:_bottomColorBackgroundView];
    }
    return _bottomColorBackgroundView;
}

- (UILabel *)gameTimeCountDownLabel {
    if (!_gameTimeCountDownLabel) {
        _gameTimeCountDownLabel = [[UILabel alloc] init];
        _gameTimeCountDownLabel.backgroundColor = [UIColor mmg_colorWithHex:kMMGColorHex_GameCountDownColor];
        
        _gameTimeCountDownLabel.text = @"30";
        _gameTimeCountDownLabel.textColor = [UIColor whiteColor];
        _gameTimeCountDownLabel.textAlignment = NSTextAlignmentCenter;
        _gameTimeCountDownLabel.font = [UIFont boldSystemFontOfSize:20];
        _gameTimeCountDownLabel.layer.cornerRadius = 15;
        _gameTimeCountDownLabel.clipsToBounds = YES;
        
        [self insertSubview:_gameTimeCountDownLabel aboveSubview:self.prizeClawGLView];
    }
    return _gameTimeCountDownLabel;
}

@end
